using System;
using System.Collections;
using System.Collections.Generic;
# if UNITY_EDITOR
using UnityEditor;
# endif
using UnityEngine;

namespace QFramework
{
    [CreateAssetMenu(menuName="ItemKit/Create item")]
    public class Item : ScriptableObject,IItem
    {
        [DisplayLabel("名称")] 
        public string Name = String.Empty;
        
        [DisplayLabel("描述")]
        [TextArea(minLines:1,maxLines:3)]
        public string Description = String.Empty;
        
        [DisplayLabel("关键字")] 
        public string Key = String.Empty;
        public Sprite Icon;

        [DisplayLabel("可堆叠")]
        public bool Stackable = true;
        
        // DisplayIf的意思是，当Stackable为true的时候，才显示下面的属性
        [DisplayIf(nameof(Stackable),false,true)]
        [DisplayLabel("    有堆叠限制")]
        public bool HasMaxStackableCount=false;
        [DisplayIf(new []{nameof(Stackable),nameof(HasMaxStackableCount)},new []{false,false},true)]
        [DisplayLabel("        最大堆叠数量")]
        public int MaxStackableCount;
        
        [DisplayLabel("是武器")] 
        public bool IsWeapon = false;

        public string GetName => ItemKit.CurrentLanguage == ItemKit.DefaultLanguage ? Name : LocaleItem.Name;
        public string GetKey => ItemKit.CurrentLanguage == ItemKit.DefaultLanguage ? Key : LocaleItem.Key;
        public string GetDescription => ItemKit.CurrentLanguage == ItemKit.DefaultLanguage ? Description : LocaleItem.Description;
        public ItemLanguagePackage.LocaleItem LocaleItem { get; set; }
        public Sprite GetIcon => Icon;
        
        /// <summary>
        /// 判断Item类别，比如装备栏的Item只能放入装备道具，其它道具无法放入
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public bool GetBoolean(string propertyName)
        {
            if (propertyName == "IsWeapon")
            {
                return IsWeapon;
            }
            return false;
        }

        public bool IsStackable =>Stackable;
        public bool GetHasMaxStackableCount => HasMaxStackableCount;
        public int GetMaxStackableCount => MaxStackableCount;
    }

# if UNITY_EDITOR
    [CustomEditor(typeof(Item))]
    public class ItemEditor : Editor
    {
        private SerializedProperty mIcon;
        private SerializedProperty mKey;
        private void OnEnable()
        {
            mIcon = serializedObject.FindProperty("Icon");
            mKey = serializedObject.FindProperty("Key");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            GUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("图标");
            //var iconPropery = serializedObject.FindProperty("Icon");
            mIcon.objectReferenceValue = EditorGUILayout.ObjectField(mIcon.objectReferenceValue,
                typeof(Sprite), true, GUILayout.Height(48),
                GUILayout.Width(48));
            GUILayout.EndHorizontal();

            serializedObject.DrawProperties(false,0,"Icon");
            if (mKey.stringValue!=target.name)
            {
                target.name = mKey.stringValue;
                EditorUtility.SetDirty(target);
            }
            serializedObject.ApplyModifiedPropertiesWithoutUndo();
        }
    }
# endif
}
